package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.*;
import cc.rengu.igas.momp.common.dao.impl.*;
import cc.rengu.igas.momp.common.entity.TMMcMchntInfo;
import cc.rengu.igas.momp.common.entity.TMMcMoneyOffInfo;
import cc.rengu.igas.momp.common.entity.TMPlatBizTmp;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.util.CommonMessageCheck;
import cc.rengu.igas.momp.common.util.MompDateUtil;
import cc.rengu.igas.momp.common.util.MompToolUtil;
import cc.rengu.igas.momp.common.util.RbdpClient;
import cc.rengu.igas.momp.facade.request.ManageMoneyOffMcRequest;
import cc.rengu.igas.momp.facade.response.ManageMoneyOffMcResponse;
import cc.rengu.jrbdp.register.realtime.client.RealTimeRegisterClient;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.dbs.Database;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 减免类活动信息管理功能实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/4/2 17:45
 */
public class ManageMoneyOffMcService extends RadpService {

    private String globalInstId;
    private String globalMcNo;
    private String globalTimeStamp;
    private String globalOprId;
    private String globalTxnDate;
    private String globalTableName = TableNameEnum.T_M_MC_MO_INFO.getTableNameCode();
    /**
     * 批量任务注册参数类型 PARAM_TYPE
     */
    private static final String MOMP_RBDP_ADDRESS = "MOMP_RBDP_ADDRESS";
    /**
     * 批量任务注册地址参数键值 PARAM_KEY
     */
    private static final String MOMP_RBDP_REGISTER_ADDRESS = "MOMP_RBDP_REGISTER_ADDRESS";

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            ManageMoneyOffMcRequest manageMoneyOffMcRequest = new ManageMoneyOffMcRequest();
            ConvertUtil.convertOutput(manageMoneyOffMcRequest);

            ManageMoneyOffMcResponse manageMoneyOffMcResponse = new ManageMoneyOffMcResponse();
            manageMoneyOffMcResponse.setHeader(manageMoneyOffMcRequest.getHeader());
            BizResponse<ManageMoneyOffMcResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(manageMoneyOffMcResponse);

            /* 判断查询类型 */
            rglog.debug("TXN_NUM=<{}>", manageMoneyOffMcRequest.getHeader().getTxnNum());

            String queryType = manageMoneyOffMcRequest.getQueryType();
            String queryRole = manageMoneyOffMcRequest.getQueryRole();
            String oprType = manageMoneyOffMcRequest.getOprType();
            globalInstId = manageMoneyOffMcRequest.getHeader().getInstId();
            globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            globalOprId = manageMoneyOffMcRequest.getQueryId();
            globalMcNo = manageMoneyOffMcRequest.getMcNo();
            globalTxnDate = manageMoneyOffMcRequest.getHeader().getTransDate();
            rglog.debug("QUERY_TYPE=<{}>", queryType);
            rglog.debug("QUERY_ROLE=<{}>", queryRole);
            rglog.debug("OPR_TYPE=<{}>", oprType);

            /* 报文检查 */
            CommonMessageCheck.checkBizMessageHeader(queryType, globalOprId, queryRole, rglog);
            CommonMessageCheck.checkSingleValue(oprType, rglog);

            // 服务调用
            BizResponse<ManageMoneyOffMcResponse> bizResponseNew = manageMoneyOffMc(
                    manageMoneyOffMcRequest, manageMoneyOffMcResponse, xmlTreeUtil);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 减免类活动管理
     *
     * @param manageMoneyOffMcRequest  减免类营销活动管理接口请求对象
     * @param manageMoneyOffMcResponse 减免类营销活动管理接口应答对象
     * @param xmlTreeUtil              内部XML树
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.ManageMoneyOffMcResponse> 减免类营销活动管理接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/29 01:38
     */
    private BizResponse<ManageMoneyOffMcResponse> manageMoneyOffMc(ManageMoneyOffMcRequest manageMoneyOffMcRequest, ManageMoneyOffMcResponse manageMoneyOffMcResponse, XmlTreeUtil xmlTreeUtil)
            throws Exception {

        BizResponse<ManageMoneyOffMcResponse> bizResponse = new BizResponse<>();

        /* 判断查询类型 */
        String queryType = manageMoneyOffMcRequest.getQueryType();
        String queryRole = manageMoneyOffMcRequest.getQueryRole();
        String oprType = manageMoneyOffMcRequest.getOprType();

//        try {
        /* 判断用户的角色和权限 */
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(queryType)) {
            if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                /* 银行审批岗-正式表和临时表所有数据 */
                if (OprTypeEnum.AUDIT_PASSED.getOprTypeCode().equals(oprType)) {
                    /* 审批通过 */
                    auditPassed(manageMoneyOffMcRequest);

                } else if (OprTypeEnum.AUDIT_REFUSED.getOprTypeCode().equals(oprType)) {
                    /* 审批拒绝 */
                    auditRefused(manageMoneyOffMcRequest);

                } else {
                    rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                    throw new BizException(RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_OPR_TYPE_ERROR.getRespDesc());
                }

            } else if (QueryRoleEnum.BANK_BASIC_QUERY_ROLE.getQueryRoleCode().equals(queryRole)) {
                if (OprTypeEnum.AUDIT_DELETE.getOprTypeCode().equals(oprType)) {
                    /* 删除草稿 */
                    deleteDraftMcData(manageMoneyOffMcRequest);
                }
            } else {
                /* 银行录入岗或银行发布岗 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
            }
        } else {
            /* 商户、代理商、收银员或用户 */
            rglog.error("当前<{}>角色的查询用户<{}>无权限查看数据!", queryRole, queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
        }


        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        manageMoneyOffMcResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        manageMoneyOffMcResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(manageMoneyOffMcResponse);

        return bizResponse;
    }

    /**
     * 删除草稿
     *
     * @param manageMoneyOffMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void deleteDraftMcData(ManageMoneyOffMcRequest manageMoneyOffMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMoneyOffMcRequest.getHeader().getInstId();
        String mcNo = manageMoneyOffMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 查询营销活动信息 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMoneyOffInfo mcMoneyOffInfo = mcDetailInfoMapper.selectMoneyOffMcDetailInfo(instId, mcNo);
        if (null == mcMoneyOffInfo) {
            /* 获取营销活动信息失败 */
            rglog.error("获取营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, mcNo);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_DETAIL_ERROR.getRespDesc());
        }

        if (!McStatusEnum.DRAFT.getMcStatusCode().equals(mcMoneyOffInfo.getMcStatus())) {
            /* 营销活动状态异常，不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), mcMoneyOffInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }

        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(mcMoneyOffInfo.getAuditStatus()) ||
                AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(mcMoneyOffInfo.getAuditStatus()) ||
                AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode().equals(mcMoneyOffInfo.getAuditStatus())) {
            /* 审批中不可删除草稿 */
            rglog.error("{}}, MC_STATUS=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc(), mcMoneyOffInfo.getMcStatus(), mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_DELETE_DRAFT_ERROR.getRespDesc());
        }
        if (mcMoneyOffInfo.getSponsor().equals(CommonConstant.SPONSOR_MCHNT)) {
            rglog.error("{}}, MC_NO=<{}>", RespCodeEnum.MC_TYPE_IS_MCHNT_NOT_ALLOWED_TO_OPERATE.getRespDesc(), mcMoneyOffInfo.getMcNo());
            throw new BizException(RespCodeEnum.MC_TYPE_IS_MCHNT_NOT_ALLOWED_TO_OPERATE.getRespCode(), RespCodeEnum.MC_TYPE_IS_MCHNT_NOT_ALLOWED_TO_OPERATE.getRespDesc());

        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 删除营销活动信息 */
        returnCode = mcDetailInfoMapper.deleteMoneyOffMcInfo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动机构信息 */
        McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
        returnCode = mcOrgInfoMapper.deleteMcOrgInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_ORG_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动商户信息 */
        if (CommonConstant.SPECIFIC_MCHNT_LIST.equals(mcMoneyOffInfo.getMerchantRange())) {
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            returnCode = mcMchntInfoMapper.deleteMcMchntInfoListByBank(instId, mcNo);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 异常结束数据库事务 */
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc(), returnCode, mcNo);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_MCHNT_DATA_ERROR.getRespDesc());
            }
        }

        /* 删除营销活动用户信息 */
        if (CommonConstant.SPECIFIC_CLIENT_SUPPORT.equals(mcMoneyOffInfo.getClientRange())) {
            McUserInfoMapper mcUserInfoMapper = new McUserInfoMapperImpl();
            returnCode = mcUserInfoMapper.deleteMcUserInfoListByBank(instId, mcNo);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 异常结束数据库事务 */
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_USER_DATA_ERROR.getRespDesc(), returnCode, mcNo);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.DELETE_MC_USER_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_USER_DATA_ERROR.getRespDesc());
            }
        }

        /* 删除营销活动时间信息 */
        if (CommonConstant.SPECIFIC_TIME_SUPPORT.equals(mcMoneyOffInfo.getMcTimeFlag())) {
            McTimeInfoMapper mcTimeInfoMapper = new McTimeInfoMapperImpl();
            returnCode = mcTimeInfoMapper.deleteMcTimeInfoListByBank(instId, mcNo);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 异常结束数据库事务 */
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_TIME_DATA_ERROR.getRespDesc(), returnCode, mcNo);
                dbsUtil.dbsEndTransaction(false);
                throw new BizException(RespCodeEnum.DELETE_MC_TIME_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_TIME_DATA_ERROR.getRespDesc());
            }
        }

        /* 删除营销活动规则信息 */
        McRuleInfoMapper mcRuleInfoMapper = new McRuleInfoMapperImpl();
        returnCode = mcRuleInfoMapper.deleteMoneyOffRuleInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_RULE_DATA_ERROR.getRespDesc());
        }

        /* 删除营销活动产品信息 */
        McProductInfoMapper mcProductInfoMapper = new McProductInfoMapperImpl();
        returnCode = mcProductInfoMapper.deleteMcProductInfoListByBank(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_MC_PRODUCT_DATA_ERROR.getRespDesc());
        }

        /* 删除临时表营销活动所有信息 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        returnCode = platBizTmpMapper.deleteTmpMcDataByMcNo(instId, mcNo);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 异常结束数据库事务 */
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespDesc(), returnCode, mcNo);
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespCode(), RespCodeEnum.DELETE_TMP_MC_DATA_ERROR.getRespDesc());
        }

        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批通过
     *
     * @param manageMoneyOffMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditPassed(ManageMoneyOffMcRequest manageMoneyOffMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMoneyOffMcRequest.getHeader().getInstId();
        String mcNo = manageMoneyOffMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        rglog.debug("INST_ID=<{}>, MC_NO=<{}>", instId, mcNo);

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMoneyOffInfo tmMcMoneyOffInfo = (TMMcMoneyOffInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        if (null == tmMcMoneyOffInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tmMcMoneyOffInfo.getMcNo());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 判断如何更新临时表数据 */
        String bizStatus = platBizTmp.getBizStatus();
        if (BizStatusEnum.TMP_STATUS.getBizStatusCode().equals(bizStatus)) {
            /* 非正式数据要更新为正式数据 */
            bizStatus = BizStatusEnum.NORMAL_STATUS.getBizStatusCode();
        }

        /* 根据现有营销活动状态判断如更新营销活动状态 */
        String mcStatus;
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(platBizTmp.getMcStatus())) {
            /* 当前营销活动状态是草稿状态,需要更新营销活动状态为未发布 */
            mcStatus = McStatusEnum.NOT_PUBLISHED.getMcStatusCode();
        } else {
            /* 当前营销活动状态不是草稿状态,不需要更新营销活动状态 */
            mcStatus = platBizTmp.getMcStatus();
        }

        /* 根据现有审批状态决定如何更新临时表数据 */
        String oldAuditStatus;
        String newAuditStatus;
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {

            oldAuditStatus = AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode();
            newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

        } else {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }

        /* 更新临时表数据 */
        returnCode = platBizTmpMapper.updateTmpMcAllStatusByMcNoAndAuditStatus(instId, mcNo, bizStatus, mcStatus, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp);
        if (Database.DBS_SUCCESS != returnCode) {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据,营销活动状态是草稿或未发布状态才需要操作正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {

            if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
                /* 如果营销活动状态是草稿状态,需要将草稿状态变更为未发布状态,同时将审批状态变更为发布待审批状态 */
                TMMcMoneyOffInfo tmMcMoneyOffInfo1 = new TMMcMoneyOffInfo();
                tmMcMoneyOffInfo1.setInstId(instId);
                tmMcMoneyOffInfo1.setMcNo(mcNo);
                tmMcMoneyOffInfo1.setMcStatus(McStatusEnum.NOT_PUBLISHED.getMcStatusCode());
                tmMcMoneyOffInfo1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
                tmMcMoneyOffInfo1.setLastOprId(globalOprId);
                tmMcMoneyOffInfo1.setUpdateTime(globalTimeStamp);
                tmMcMoneyOffInfo1.setAuditRefuseReason(manageMoneyOffMcRequest.getOprContent());

                returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMoneyOffInfo1, globalTableName);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            } else {
                /* 如果营销活动状态是未发布状态,需要将审批状态变更为发布待审批状态 */
                if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus())) {
                    /* 新增审批流程 */
                    oldAuditStatus = AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode();
                    newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

                } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus())) {
                    /* 修改审批审批流程 */
                    oldAuditStatus = AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode();
                    newAuditStatus = AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode();

                } else {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
                }
                /* 更新正式表数据 */
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo, newAuditStatus, oldAuditStatus, globalOprId, globalTimeStamp, tmMcMoneyOffInfo.getAuditRefuseReason());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }
            }
            //查询商户信息表是否有数据
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            TMMcMchntInfo tmMcMchntInfo = mcMchntInfoMapper.selectMcMchntInfoListCount(tmMcMoneyOffInfo.getInstId(), tmMcMoneyOffInfo.getMcNo());
            int mcMchntListSize = Integer.valueOf(tmMcMchntInfo.getRemark5());
            if ((tmMcMoneyOffInfo.getMerchantRange().equals(CommonConstant.SPECIFIC_MCHNT_LIST) && mcMchntListSize == CommonConstant.ZERO)
                    || tmMcMoneyOffInfo.getMerchantRange().equals(CommonConstant.MCHNT_LAST_MONTH_AVG_DEPOSIT) ||
                    tmMcMoneyOffInfo.getMerchantRange().equals(CommonConstant.MCHNT_LAST_MONTH_AVG_TXN_AMOUNT)) {
                //注册定时任务
                timingTask(CommonConstant.MO_CP_TIME_TASK_NAME, globalTxnDate, "", tmMcMoneyOffInfo.getInstId(), tmMcMoneyOffInfo.getMcNo());
            }
        } else if (McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) &&
                platBizTmp.getBizStatus().equals(BizStatusEnum.TMP_STATUS.getBizStatusCode()) &&
                platBizTmp.getMcStatus().equals(McStatusEnum.DRAFT.getMcStatusCode()) &&
                (platBizTmp.getAuditStatus().equals(AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode()) || platBizTmp.getAuditStatus().equals(AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode()))) {
            int returnCodes = mcDetailInfoMapper.updateMcAllStatusByMcNoAndOriStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo, McStatusEnum.PROGRESSING.getMcStatusCode(), McStatusEnum.PROGRESSING.getMcStatusCode(),
                    oldAuditStatus, AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
            if (returnCodes != 0) {
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
            }

            //更新正式表
            TMMcMoneyOffInfo tmMcMoneyOffInfo1 = new TMMcMoneyOffInfo();
            tmMcMoneyOffInfo1.setInstId(instId);
            tmMcMoneyOffInfo1.setMcNo(mcNo);
            tmMcMoneyOffInfo1.setMcStatus(McStatusEnum.PROGRESSING.getMcStatusCode());
            tmMcMoneyOffInfo1.setAuditStatus(AuditStatusEnum.PUBLISH_AUDIT_PROCESSING.getAuditStatusCode());
            tmMcMoneyOffInfo1.setLastOprId(globalOprId);
            tmMcMoneyOffInfo1.setUpdateTime(globalTimeStamp);

            returnCode = mcDetailInfoMapper.updateMcStatusForPublishing(tmMcMoneyOffInfo1, globalTableName);
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else {
            /* 营销活动状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_PASS_AUDIT_ERROR.getRespDesc());
        }
        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 审批拒绝
     *
     * @param manageMoneyOffMcRequest 减免类营销活动管理接口请求对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/4/7 00:53
     */
    private void auditRefused(ManageMoneyOffMcRequest manageMoneyOffMcRequest) throws Exception {

        int returnCode = 0;
        String instId = manageMoneyOffMcRequest.getHeader().getInstId();
        String mcNo = manageMoneyOffMcRequest.getMcNo();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        /* 营销活动信息正式表查询 */
        McDetailInfoMapper mcDetailInfoMapper = new McDetailInfoMapperImpl();
        TMMcMoneyOffInfo tmMcMoneyOffInfo = (TMMcMoneyOffInfo) mcDetailInfoMapper.selectMcDetailInfo(instId, mcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        if (null == tmMcMoneyOffInfo) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, INST_ID=<{}>, MC_NO=<{}>", returnCode, instId, mcNo);
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 营销活动信息临时表查询 */
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        TMPlatBizTmp platBizTmp = platBizTmpMapper.selectTmpMcInfo(instId, mcNo, TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
        if (null == platBizTmp) {
            /* 异常结束数据库事务 */
            rglog.error("查询营销活动信息失败, RETURN_CODE=<{}>, MC_NO=<{}>", returnCode, tmMcMoneyOffInfo.getMcNo());
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }

        /* 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();

        /* 判断如何更新临时表数据 */
        if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
                    AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode());
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            returnCode = platBizTmpMapper.updateTmpMcAuditStatusByMcNoAndAuditStatus(instId, mcNo,
                    AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode());
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }

        } else if ((AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) || AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) && McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode());
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MO_INFO.getTableNameDesc());
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
                returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
                returnCode = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            }
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
            int returnCodes = 0;
            tmPlatBizTmp.setBizStatus(BizStatusEnum.TMP_STATUS.getBizStatusCode());
            tmPlatBizTmp.setMcStatus(McStatusEnum.DRAFT.getMcStatusCode());
            tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameCode());
            tmPlatBizTmp.setOprTableCname(TableNameEnum.T_M_MC_MCHNT_INFO.getTableNameDesc());
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode());
                returnCodes = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                tmPlatBizTmp.setAuditStatus(AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode());
                returnCodes = platBizTmpMapper.updataTMPlatBizByinstIdMcNoTableNameAuditStatus(instId, mcNo, tmPlatBizTmp, AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(), "", "");
            }
            if (Database.DBS_SUCCESS != returnCodes && Database.DBS_NOT_FOUND != returnCodes) {
                /* 营销活动临时数据审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
        } else {
            /* 营销活动临时数据审批状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_TMP_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }

        /* 判断如何更新正式表数据 */
        if (McStatusEnum.DRAFT.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus()) || McStatusEnum.NOT_PUBLISHED.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus()) && !McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMoneyOffMcRequest.getOprContent());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else if (AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(tmMcMoneyOffInfo.getAuditStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMoneyOffMcRequest.getOprContent());
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                }

            } else {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_AUDIT_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
            }

        } else if ((AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()) || AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus()))
                && McStatusEnum.PROGRESSING.getMcStatusCode().equals(tmMcMoneyOffInfo.getMcStatus())) {
            //进行中商户增加审批拒绝  正式表改为新增拒绝状态
            if (AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode().equals(platBizTmp.getAuditStatus())) {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.NEW_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.NEW_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMoneyOffMcRequest.getOprContent());
            } else {
                returnCode = mcDetailInfoMapper.updateMcAuditStatusByMcNoAndAuditStatusAndTableName(TableNameEnum.T_M_MC_MO_INFO.getTableNameCode(), instId, mcNo,
                        AuditStatusEnum.MODIFY_AUDIT_REJECT.getAuditStatusCode(), AuditStatusEnum.MODIFY_AUDIT_PROCESSING.getAuditStatusCode(),
                        globalOprId, globalTimeStamp, manageMoneyOffMcRequest.getOprContent());
            }
            if (Database.DBS_SUCCESS != returnCode) {
                /* 营销活动审批状态异常，不可审批通过 */
                dbsUtil.dbsEndTransaction(false);
                rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc(), returnCode, mcNo);
                throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
            }
            //查询营销商户表数据
            McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
            List<TMMcMchntInfo> tmMcMchntInfoList = getAllMcMchntListInfoByinstIdMcMoFlag(CommonConstant.MC_EXIT_FLAG, CommonConstant.MCHNT_NEW_FLAG);
            if (tmMcMchntInfoList != null && tmMcMchntInfoList.size() > 0) {
                for (TMMcMchntInfo tmMcMchntInfo : tmMcMchntInfoList) {
                    tmMcMchntInfo.setNewMchntFlag(CommonConstant.MCHNT_REFUSE_FLAG);
                }
                returnCode = mcMchntInfoMapper.updateMcMchntInfoListByBank(tmMcMchntInfoList);
                if (Database.DBS_SUCCESS != returnCode) {
                    /* 营销活动审批状态异常，不可审批通过 */
                    dbsUtil.dbsEndTransaction(false);
                    rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{},营销活动商户正式表数据更新失败>", RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc(), returnCode, mcNo);
                    throw new BizException(RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespCode(), RespCodeEnum.WRONG_UPDATEMCMCHNT_ERROR.getRespDesc());
                }
            }
        } else {
            /* 营销活动状态异常，不可审批通过 */
            dbsUtil.dbsEndTransaction(false);
            rglog.error("{}, RETURN_CODE=<{}>, MC_NO=<{}>", RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc(), returnCode, mcNo);
            throw new BizException(RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespCode(), RespCodeEnum.WRONG_MC_STATUS_TO_REFUSE_AUDIT_ERROR.getRespDesc());
        }


        /* 正常结束数据库事务 */
        dbsUtil.dbsEndTransaction(true);
    }

    /**
     * 取待提交发布营销活动商户规则临时表中待提交发布的商户数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMPlatBizTmp> 业务数据临时表
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/25 23:07
     */
    private List<TMPlatBizTmp> selectNewAddedMchntInfoForPublishing(TMPlatBizTmp tmPlatBizTmp) throws Exception {

        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        return platBizTmpMapper.selectNewAddedMchntInfoForPublishing(tmPlatBizTmp);
    }

    /**
     * 将临时表新增商户数据插入正式表
     *
     * @param tmMcMchntInfoList 营销活动商户信息表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/26 00:16
     */
    private int insertMcMchntInfoListByBank(List<TMMcMchntInfo> tmMcMchntInfoList) throws Exception {

        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        return mcMchntInfoMapper.insertMcMchntInfoListByBank(tmMcMchntInfoList);
    }

    /**
     * 注册定时任务
     *
     * @param taskName   任务名称
     * @param settleDate 清算日期 不上送 默认当前日期
     * @param regTime    执行日期 不上送 立即执行
     * @param taskBatch  批次号后五位清算日期内唯一
     * @param taskParam  营销活动编号
     * @throws Exception
     */
    private void timingTask(String taskName, String settleDate, String regTime, String taskBatch, String taskParam) throws Exception {
        taskBatch = taskBatch.concat(MompToolUtil.getAppoint(MompDateUtil.getTime(), 0, 5));
        rglog.info("任务名称<{}> 清算日期<{}> 执行时间<{}> 批次号<{}> 营销活动编号<{}>", taskName, settleDate, regTime, taskBatch, taskParam);
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(AppParamConstant.DEFAULT_INSTID, MOMP_RBDP_ADDRESS, MOMP_RBDP_REGISTER_ADDRESS);
        if (null == sysParam) {
            rglog.error("获取批量任务注册地址失败！");
            throw new BizException(RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespCode(), RespCodeEnum.FAILED_TO_GET_RBDP_REGISTER_ADDRESS.getRespDesc());
        }
        //批量注册地址
        String rbdpserver = sysParam.getParamValue();
        RealTimeRegisterClient a = RbdpClient.getIntance1(rglog, rbdpserver);

        int ret = a.registerTask(taskName, settleDate, settleDate.concat(DateUtil.getCurrentTime()), taskBatch, taskParam);
        rglog.info("实时任务注册返回：ret {}", ret);
        String result = "";
        if (0 != ret) {
            result = "注册失败: " + RbdpClient.getIntance1(rglog, rbdpserver).getError();
            rglog.error("实时任务注册返回：ret {}", result);
            throw new BizException(RespCodeEnum.FAILED_TO_TIME_TASK.getRespCode(), RespCodeEnum.FAILED_TO_TIME_TASK.getRespDesc());
        } else {
            result = "注册成功.";
            rglog.info("实时任务注册返回：ret {}", result);
        }
    }

    /**
     * 获取营销活动商户信息列表(正式表)
     *
     * @param exitFlag     退出标志
     * @param newMchntFlag 新商户标志
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntInfo> 营销活动商户信息表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/8/10 18:35
     */
    private List<TMMcMchntInfo> getAllMcMchntListInfoByinstIdMcMoFlag(String exitFlag, String newMchntFlag) throws Exception {
        List<Object> tmMcMchntInfoList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo();
        int i = 0;
        pageInfo.setPageIndex(i);
        pageInfo.setPageSize(9999);
        TMMcMchntInfo tmMcMchntInfo = new TMMcMchntInfo();
        tmMcMchntInfo.setInstId(globalInstId);
        tmMcMchntInfo.setMcNo(globalMcNo);
        McMchntInfoMapper mcMchntInfoMapper = new McMchntInfoMapperImpl();
        PageResult tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoListByinstIdMcMoFlag(globalInstId, globalMcNo, exitFlag, newMchntFlag, pageInfo);
        if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
            tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
        }
        while (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult() && tmMcMchntInfoPageInfo.getResult().size() >= 1) {
            i++;
            pageInfo.setPageIndex(i);
            tmMcMchntInfoPageInfo = mcMchntInfoMapper.selectMcMchntInfoListByinstIdMcMoFlag(globalInstId, globalMcNo, exitFlag, newMchntFlag, pageInfo);
            if (null != tmMcMchntInfoPageInfo && null != tmMcMchntInfoPageInfo.getResult()) {
                tmMcMchntInfoList.addAll(tmMcMchntInfoPageInfo.getResult());
            }
        }
        List<TMMcMchntInfo> mcMchntInfoList = tmMcMchntInfoList.stream().map(item -> (TMMcMchntInfo) (item)).collect(Collectors.toList());
        return mcMchntInfoList;
    }


}
